import React from 'react';
import BaseModule from "../common/BaseModule";
import Service from "./Service";
import Lng from "../common/lng";
import {Popconfirm} from "antd";
import AddIntervalForm from "./AddIntervalForm";
import ModalTips from "../common/ModalTips";
import EditIntervalForm from "./EditIntervalForm";

class DishInterval extends BaseModule {

    constructor(props) {
        super(props);
        this.state.id = "";

        this._page.unionType = {
            "pre_dish_name": "OR",
            "next_dish_code": "OR",
            "next_dish_name": "OR"
        }
    }

    loadData() {
        this._page.items = [];
        const value = this.refs.input.input.value;
        this._page.items.push({
            queryKey: "pre_dish_code",
            queryValue: value,
            conditionType: "LIKE"
        }, {
            queryKey: "pre_dish_name",
            queryValue: value,
            conditionType: "LIKE"
        }, {
            queryKey: "next_dish_code",
            queryValue: value,
            conditionType: "LIKE"
        }, {
            queryKey: "next_dish_name",
            queryValue: value,
            conditionType: "LIKE"
        });

        super.superLoadData(Service.listInterval);
    }

    edit(record) {
        this.setState({
            id: record.id
        }, () => {
            this.refs["intervalEdit"].showModal();
        });
    }

    addClick() {
        this.refs["dishInterval"].showModal();
    }

    remove(record) {
        const self = this;
        Service.removeInterval(record.id, () => {
            self.loadData();
        })
    }

    validate(values) {
        if (values.preDishCode == values.nextDishCode) {
            ModalTips.error({
                content: Lng.get("dishTypeInterval.cntSame")
            });
            return false;
        }
        return true;
    }

    render() {
        const columns = [{
            title: Lng.get("dishTypeInterval.preDishCode"),
            dataIndex: 'preDishCode',
            key: 'preDishCode',
        }, {
            title: Lng.get("dishTypeInterval.preDishName"),
            dataIndex: 'preDishName',
            key: 'preDishName',
        }, {
            title: Lng.get("dishTypeInterval.nextDishCode"),
            dataIndex: 'nextDishCode',
            key: 'nextDishCode',
        }, {
            title: Lng.get("dishTypeInterval.nextDishName"),
            dataIndex: 'nextDishName',
            key: 'nextDishName',
        }, {
            title: Lng.get("dishTypeInterval.interval"),
            dataIndex: 'constraintTime',
            key: 'constraintTime',
            render: (text, record) => {
                return (record.constraintTime || "-") + " " + Lng.get("common." + record.constraintTimeUnit);
            }
        }, {
            title: Lng.get("common.operation"),
            dataIndex: 'operation',
            key: 'operation',
            render: (text, record, index) => {
                return <div className={"operation"}>
                    <a onClick={this.edit.bind(this, record)}>{Lng.get("common.edit", {name: ""})}</a>
                    <Popconfirm title={Lng.get("common.confirm")} onConfirm={this.remove.bind(this, record)}
                                okText={Lng.get("common.ok")}
                                cancelText={Lng.get("common.cancel")}>
                        <a href={"#"}>{Lng.get("common.remove")}</a>
                    </Popconfirm>
                </div>
            }
        }];
        return (
            <div id={"dish-interval-content"}>
                {this.renderSearchBar(Lng.get("dishTypeInterval.placeholder"))}
                {this.renderTable(columns)}
                {this.renderModal("dishInterval", AddIntervalForm, Lng.get("dishTypeInterval.main"), Service.addInterval, null, this.validate)}
                {this.renderEditModal("intervalEdit", EditIntervalForm, Lng.get("dishTypeInterval.main"), this.state.id, Service.updateInterval)}
            </div>

        )
    }
}

export default DishInterval;